package com.liit.front.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liit.front.filter.WebContext;
import com.liit.module.dto.PathDto;
import com.liit.module.entity.Brand;
import com.liit.module.entity.Files;
import com.liit.module.frame.utils.FileUtil;
import com.liit.module.service.BrandService;
import com.liit.module.service.FilesService;

/**
 * @author dingweibo
 * @Title: BrandController.java
 * @Description: 品牌故事
 * @date 2015年12月2日
 */
@Controller
@RequestMapping(value = "/brand")
public class BrandController {
	@Resource
	BrandService brandService;

	@Resource
	FilesService filesService;

	/**
	 * 品牌项
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}")
	public String item(@PathVariable Integer id) {
		if (id == null) {
			throw new NullPointerException("未找到品牌信息");
		}
		Brand brand = brandService.selectById(id);
		if (brand == null) {
			throw new NullPointerException("未找到品牌信息");
		}
		if (brand.getVideoPath() != null) {
			Files files = filesService.selectById(brand.getVideoPath());
			WebContext.setAttribute("videoPath", FileUtil.getUrl(files));
		}
		if (brand.getImgOne() != null) {
			Files files = filesService.selectById(brand.getImgOne());
			WebContext.setAttribute("imgOne", FileUtil.getUrl(files));
		}
		if (brand.getImgTwo() != null) {
			Files files = filesService.selectById(brand.getImgTwo());
			WebContext.setAttribute("imgTwo", FileUtil.getUrl(files));
		}
		if (brand.getImgThree() != null) {
			Files files = filesService.selectById(brand.getImgThree());
			WebContext.setAttribute("imgThree", FileUtil.getUrl(files));
		}
		if (brand.getImgFour() != null) {
			Files files = filesService.selectById(brand.getImgFour());
			WebContext.setAttribute("imgFour", FileUtil.getUrl(files));
		}
		if (brand.getImgFive() != null) {
			Files files = filesService.selectById(brand.getImgFive());
			WebContext.setAttribute("imgFive", FileUtil.getUrl(files));
		}
		if (brand.getImgSix() != null) {
			Files files = filesService.selectById(brand.getImgSix());
			WebContext.setAttribute("imgSix", FileUtil.getUrl(files));
		}
		if (brand.getImgSeven() != null) {
			Files files = filesService.selectById(brand.getImgSeven());
			WebContext.setAttribute("imgSeven", FileUtil.getUrl(files));
		}
		if (brand.getImgEight() != null) {
			Files files = filesService.selectById(brand.getImgEight());
			WebContext.setAttribute("imgEight", FileUtil.getUrl(files));
		}
		if (brand.getImgNine() != null) {
			Files files = filesService.selectById(brand.getImgNine());
			WebContext.setAttribute("imgNine", FileUtil.getUrl(files));
		}
		if (brand.getImgTen() != null) {
			Files files = filesService.selectById(brand.getImgTen());
			WebContext.setAttribute("imgTen", FileUtil.getUrl(files));
		}
		if (brand.getImgEleven() != null) {
			Files files = filesService.selectById(brand.getImgEleven());
			WebContext.setAttribute("imgEleven", FileUtil.getUrl(files));
		}
		if (brand.getImgTwelve() != null) {
			Files files = filesService.selectById(brand.getImgTwelve());
			WebContext.setAttribute("imgTwelve", FileUtil.getUrl(files));
		}
		if (brand.getImgThirteen() != null) {
			Files files = filesService.selectById(brand.getImgThirteen());
			WebContext.setAttribute("imgThirteen", FileUtil.getUrl(files));
		}
		if (brand.getImgFourteen() != null) {
			Files files = filesService.selectById(brand.getImgFourteen());
			WebContext.setAttribute("imgFourteen", FileUtil.getUrl(files));
		}
		if (brand.getImgFifteen() != null) {
			Files files = filesService.selectById(brand.getImgFifteen());
			WebContext.setAttribute("imgFifteen", FileUtil.getUrl(files));
		}
		if (brand.getImgSixteen() != null) {
			Files files = filesService.selectById(brand.getImgSixteen());
			WebContext.setAttribute("imgSixteen", FileUtil.getUrl(files));
		}
		if (brand.getImgSeventeen() != null) {
			Files files = filesService.selectById(brand.getImgSeventeen());
			WebContext.setAttribute("imgSeventeen", FileUtil.getUrl(files));
		}
		if (brand.getImgEighteen() != null) {
			Files files = filesService.selectById(brand.getImgEighteen());
			WebContext.setAttribute("imgEighteen", FileUtil.getUrl(files));
		}
		if (brand.getImgNineteen() != null) {
			Files files = filesService.selectById(brand.getImgNineteen());
			WebContext.setAttribute("imgNineteen", FileUtil.getUrl(files));
		}
		if (brand.getImgTwenty() != null) {
			Files files = filesService.selectById(brand.getImgTwenty());
			WebContext.setAttribute("imgTwenty", FileUtil.getUrl(files));
		}
		WebContext.setAttribute("brand", brand);
		// 查询其它品牌
		List<Brand> list = brandService.listOther(Arrays.asList(brand.getId()));
		if (!CollectionUtils.isEmpty(list)) {
			List<PathDto> paths = new ArrayList<PathDto>();
			for (Brand b : list) {
				PathDto p = new PathDto();
				p.setId(b.getId());
				p.setName(b.getName());
				p.setContent(b.getContent());
				p.setPath(FileUtil.getUrl(filesService.selectById(b.getImgOtherLogo())));
				paths.add(p);
			}
			WebContext.setAttribute("otherBrand", paths);
		}
		IndexController.navigation();
		return "/brand/brandItem";
	}
}
